from upload_common import read_data_from_stdin
from upload_common import data_is_http_link
from upload_common import handle_binary_data
from upload_common import write_data_to_stdout

data = read_data_from_stdin()
if data_is_http_link(data):
    write_data_to_stdout(data)
else:
    try:
        link = handle_binary_data(data)
        if len(link) != 0:  # 不是空的说明上传成功了，获取到了http链接
            write_data_to_stdout(link.encode("utf-8"))
        else:
            write_data_to_stdout(data)
    except Exception as e:
        write_data_to_stdout(data)